package com.niuxiaofa.newlaw.Fragment;

import android.graphics.Color;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.niuxiaofa.newlaw.Activity.AnswerQuestionsSignActivity;
import com.niuxiaofa.newlaw.Activity.ConnoisseurActivity;
import com.niuxiaofa.newlaw.Activity.ConsultationActivity;
import com.niuxiaofa.newlaw.Activity.InfomationActivity;
import com.niuxiaofa.newlaw.Activity.InfomationDetailsActivity;
import com.niuxiaofa.newlaw.Activity.ServiceActivity;
import com.niuxiaofa.newlaw.Activity.TouristActivity;
import com.niuxiaofa.newlaw.Base.BaseFragment;
import com.niuxiaofa.newlaw.Bean.BannerBean;
import com.niuxiaofa.newlaw.Bean.NewListBean;
import com.niuxiaofa.newlaw.Bean.NewOrderBean;
import com.niuxiaofa.newlaw.Network.NewsPresenter;
import com.niuxiaofa.newlaw.Network.PublicPresenter;
import com.niuxiaofa.newlaw.Network.WisdomLibraryPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.RecyclerViewHelp;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.View.ShimmerTextView;
import com.niuxiaofa.newlaw.adapter.BannerCreator;
import com.niuxiaofa.newlaw.adapter.NewsAdapter;
import com.niuxiaofa.newlaw.adapter.NoticeAdapter;
import com.niuxiaofa.newlaw.adapter.NoticePollRecyclerView;
import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.IndicatorView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class WisdomLibraryFragment extends BaseFragment implements NewsPresenter.interface_getStartBannerByType,PublicPresenter.interface_newOrder,WisdomLibraryPresenter.interface_newList{


    @BindView(R.id.banner)
    Banner banner;

    @BindView(R.id.noticePollRecyclerView)
    NoticePollRecyclerView noticePollRecyclerView;

    NoticeAdapter noticeAdapter;

    List<NewOrderBean.DataDTO.RecordsDTO> recordsDTOList=new ArrayList<>();

    @BindView(R.id.shimmertextview)
    ShimmerTextView shimmertextview;


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    List<NewListBean.DataDTO> newsList=new ArrayList<>();
    NewsAdapter newsAdapter;


    @BindView(R.id.consultation)
    RelativeLayout consultation;


    @BindView(R.id.service)
    RelativeLayout service;

    @BindView(R.id.connoisseur)
    RelativeLayout connoisseur;


    @BindView(R.id.reward)
    LinearLayout reward;

    @BindView(R.id.more)
    TextView more;

    @BindView(R.id.ykvq)
    RelativeLayout ykvq;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wisdom;
    }

    @Override
    public void initData() {

        Map map=new HashMap();
        map.put("bannerType","2");
        NewsPresenter.bannerListByType(map,this);

        PublicPresenter.newOrder(this);

        WisdomLibraryPresenter.newList(this);


        newsAdapter=new NewsAdapter(newsList, new NewsAdapter.Onclick() {
            @Override
            public void onclick(int c) {
                String[][] a=new String[][]{{"id",newsList.get(c).getId()}};
                ToActivityUtil.toNextActivity(InfomationDetailsActivity.class,a);
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(UIUtils.getContext(),RecyclerView.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                // 直接禁止垂直滑动
                return false;
            }
        };



        RecyclerViewHelp.setRecycler(recyclerview,5).setAdapter(newsAdapter);


        noticeAdapter  = new NoticeAdapter(recordsDTOList);
        RecyclerViewHelp.setRecycler(noticePollRecyclerView,10).setAdapter(noticeAdapter);


         initOnclick();
    }

    private void initOnclick() {
        consultation.setOnClickListener(e->{

            // ToActivityUtil.toNextActivity(UpVideoActivity.class);
          ToActivityUtil.toNextActivity(ConsultationActivity.class);

        });
        service.setOnClickListener(e->{
            ToActivityUtil.toNextActivity(ServiceActivity.class);

        });

        connoisseur.setOnClickListener(e->{
            ToActivityUtil.toNextActivity(ConnoisseurActivity.class);
        });

        reward.setOnClickListener(e->{
            ToActivityUtil.toNextActivity(AnswerQuestionsSignActivity.class);
        });
        more.setOnClickListener(e->{
            ToActivityUtil.toNextActivity(InfomationActivity.class);
        });
        ykvq.setOnClickListener(e->{
            ToActivityUtil.toNextActivity(TouristActivity.class);
        });


    }


    @Override
    public void addData(BannerBean u) {
        if (u.getCode()==1){
            //使用内置Indicator
            IndicatorView indicator = new IndicatorView(getContext())
                    .setIndicatorColor(Color.DKGRAY)
                    .setIndicatorSelectorColor(Color.WHITE);

            banner.setIndicator(indicator).setHolderCreator(new BannerCreator(new BannerCreator.Onclick() {
                @Override
                public void click(int id) {
//                    String[][] a = new String[][]{{"url", u.getData().get(id).getTargetUrl()}, {"title", u.getData().get(id).getSTitle()}};
//                    ToActivityUtil.toNextActivity(WebViewActivity.class, a);
                }
            })).setPages(u.getData());

        }


    }

    @Override
    public void getResponse(NewOrderBean u) {
        if (u.getCode()==1){
            recordsDTOList.addAll(u.getData().getRecords());
            noticePollRecyclerView.start();
            noticeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getResponse(NewListBean u) {
        if (u.getCode()==1){
            newsList.addAll(u.getData());
            newsAdapter.notifyDataSetChanged();
        }
    }
}
